<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82882996 15899929162
// +----------------------------------------------------------------------
namespace app\common\dao\piles;

use think\db\BaseQuery;
use app\common\dao\BaseDao;
use app\common\model\piles\PilesFaultyLogModel;

class PilesFaultyLogDao extends BaseDao
{

    /**
     * @return PilesFaultyLogModel
     */
    protected function getModel(): string
    {
        return PilesFaultyLogModel::class;
    }

    /**
     * @param array $where
     * @return BaseQuery
     */
    public function search(array $where, int $propertyId = null)
    {
        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['piles_id']) && $where['piles_id'] !== '', function ($query) use ($where) {
                $query->where('piles_id', (int)$where['piles_id']);
            })
            ->when(isset($where['user_id']) && $where['user_id'] !== '', function ($query) use ($where) {
                $query->where('user_id', (int)$where['user_id']);
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('fault_msg|handle_remark', '%' . trim($where['keywords']) . '%');
            });
    }

    public function deleteByPilesId(int $id)
    {
        return $this->getSearch([
            'piles_id' => $id
        ])->delete();
    }
}
